define(() => {
    /**
     * 根据Json里的数据构造Echarts地图所需要的数据
     * @param {} mapJson
     */
    function initMapData(mapJson) {
        var mapData = [];
        for (var i = 0; i < mapJson.features.length; i++) {
            mapData.push({
                name: mapJson.features[i].properties.name,
                //id:mapJson.features[i].id
                value:Math.round(Math.random() * 100)
            })
        }
        return mapData;
    }
    /**
     * Echarts地图
     */

//中国地图（第一级地图）的ID、Name、Json数据
// var chinaId = 610000;
// var chinaName = '陕西省'
    var chinaJson = null;

//记录父级ID、Name
    var mapStack = [];
    var parentId = null;
    var parentName = null;

//Echarts地图全局变量，主要是在返回上级地图的方法中会用到
    var myChart = null;
    function mapChart(divid,chinaId,chinaName) {

        $.get('view/maparea/asset/json/map/' + chinaId + '.json', function (mapJson) {
            chinaJson = mapJson;
            myChart = echarts.init(document.getElementById(divid));
            registerAndsetOption(myChart, chinaId, chinaName, mapJson, false)
            parentId = chinaId;
            parentName = chinaName
        })
    }

    /**
     *
     * @param {*} myChart
     * @param {*} id        省市县Id
     * @param {*} name      省市县名称
     * @param {*} mapJson   地图Json数据
     * @param {*} flag      是否往mapStack里添加parentId，parentName
     */
    function registerAndsetOption(myChart, id, name, mapJson, flag) {

        echarts.registerMap(name, mapJson);
        var max = 100, min = 0;
        myChart.setOption({
            tooltip:{
                show:true,
                formatter: function(params) {
                    console.log(params)
                    return params.name + "：" + params.value;
                }
            },
            visualMap: {
                min: 0,
                max: 100,
                left: '20',
                bottom: '50',
                text: ['100', '0'],
                calculable: false,
                inRange: {
                    color: [ "#1b5fe6", "#07c8e1"]
                },
                textStyle: {
                    color: '#ffffff',
                    fontSize: 14
                },
                itemWidth: 30,
                itemHeight: 150,
            },
            geo: {
                show: true,
                map: name,
            },
            series: [
                {
                    center: "center",
                    map: name,
                    data: initMapData(mapJson),
                    label: {
                        normal: {
                            show: true,
                            color:"#fff",
                            fontSize: 14,
                        }
                    },
                    itemStyle: {
                        normal: {
                            areaColor: '#062051',
                            borderColor: '#00f4fe',
                            borderWidth: 1,
                            shadowColor: 'rgba(255, 217, 248, 0.1)',
                            shadowOffsetX: 12,
                            shadowOffsetY: 12,
                            shadowBlur: 1
                        },
                        emphasis: {
                            areaColor: '#00f4fe',
                        }
                    },
                    roam: false,
                    type: "map"
                }
            ]
        });

        if (flag) {//往mapStack里添加parentId，parentName,返回上一级使用
            mapStack.push({
                mapId: parentId,
                mapName: parentName
            });
            parentId = id;
            parentName = name;
        }
    }

    return {
        initMapData,
        myChart,
        mapChart,
        registerAndsetOption,
    }
});

